import {Component, Injector, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzModalRef} from 'ng-zorro-antd/modal';
import {EmployeeServiceSvr} from '../../../../../services/employee.service';
import {CoreComponent} from 'pengesoft-ng-lib';
import {Resume} from '../../../../../domains/resume.domain';
import {ResumeMgeSvr} from '../../../../../services/resume-mge-svr.service';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-entry-modal',
  templateUrl: './entry-modal.component.html',
  styleUrls: ['./entry-modal.component.less']
})
export class EntryModalComponent extends  CoreComponent implements OnInit {

  entryData: any;
  entryState: string;
  title: any;
  validateForm!: FormGroup;
  listOfDataPersonChange: { jobId: any; skay: number; phone: any; name: any; id: any }[] = [];
  data: { jobId: any; skay: number; phone: any; name: any; id: any }[] = [];
  constructor(private fb: FormBuilder,
              private modal: NzModalRef,
              private employSvr: EmployeeServiceSvr,
              private injector: Injector,
              private resumeMgeSvr: ResumeMgeSvr,
              private message: NzMessageService) {
    super(injector);
  }

  ngOnInit(): void {
  }


  // 职位异动的添加修改按钮
  handleOk(e: any, validateForm, state): void {
    const sendDate = new Resume({
        Id: this.entryData.id,
        JobId: this.entryData.jobId,
        Name: this.entryData.name,
        Education: this.entryData.Education,
        Tel: this.entryData.phone,
        Email: this.entryData.Email,
        InfoSource: this.entryData.InfoSource,
        ApplyWay: this.entryData.ApplyWay,
        ApplyType: this.entryData.ApplyType,
        ApplyStatus: this.entryData.title,
        ResumeFile: this.entryData.ResumeFile,
        Remarks: this.entryData.Remarks}
    );

    if (state === '办理入职') {
      this.employSvr.addEmployee(sendDate).then(res => {
        if (res === 1) {
          this.message.success('操作成功！');
        }else {
          this.message.success('操作失败！');
        }
        this.resumeMgeSvr.getDetailList(new Resume({ApplyStatus: 5})).then(res => {
          // tslint:disable-next-line:triple-equals
          this.listOfDataPersonChange = this.dealRuZhi(res);
          this.modal.destroy(this.listOfDataPersonChange);
        });
      });

    }else if (state === '办理转正'){
      this.employSvr.update(this.entryData.id, '转正').then(res => {
        if (res === 1) {
          this.message.success('操作成功！');
        }else {
          this.message.success('操作失败！');
        }
        this.employSvr.myGetEmployeeDtoByState( '试用' ).then(res => {
          this.listOfDataPersonChange = this.dealData(res);
          this.modal.destroy(this.listOfDataPersonChange);
        });
      });
    }else if (state === '办理离职'){
      this.employSvr.update(this.entryData.id, '离职').then(res => {
        if (res === 1) {
          this.message.success('操作成功！');
        }else {
          this.message.success('操作失败！');
        }
        this.employSvr.myGetEmployeeDtoByState( '待离职' ).then(res => {
          this.listOfDataPersonChange = this.dealData(res);
          this.modal.destroy(this.listOfDataPersonChange);
        });
      });
    }
  }

  // 取消按钮
  handleCancel(e: MouseEvent): void {
    this.modal.destroy();
  }

  // 处理入职数据
  dealRuZhi(res): any {
    return res.map((item, index) => {
      return {
        skay: index + 1,
        phone: item.Resume.Tel,
        name: item.Resume.Name,
        id: item.Resume.Id,
        jobId: item.Resume.JobId,
        Email: item.Resume.Email,
        Education: item.Resume.Education,
        Remarks: item.Resume.Remarks,
        InfoSource: item.Resume.InfoSource,
        JobName: item.JobName,
      };
    });
  }

  // 处理数据
  dealData(res): any {
    return res.map((items, index) => {
      const sex = items.Employee.sex === 0 ? '男' : '女';
      return {
        skay: index + 1,
        phone: items.Employee.Phone,
        name: items.Employee.Name,
        id: items.Employee.Id,
        department: items.DeptName,
        jobId: items.Employee.JobId,
        ResumeFile: items.Employee.ResumeFile,
        ApplyStatus: items.Employee.ApplyStatus,
        ApplyType: items.Employee.ApplyType,
        ApplyWay: items.Employee.ApplyWay,
      };
    });
  }

}
